import request from '@/utils/request'

// 渠道相关接口

// 获取渠道状态列表
export const getChannelStatus = () => {
  return request({
    url: '/api/channel/status',
    method: 'get'
  })
}

// 测试渠道连接
export const testChannelConnection = (channelId: string) => {
  return request({
    url: `/api/channel/test-connection/${channelId}`,
    method: 'post'
  })
}

// 获取渠道连接日志
export const getChannelLogs = (channelId: string) => {
  return request({
    url: `/api/channel/logs/${channelId}`,
    method: 'get'
  })
}

// 获取渠道评估数据
export const getChannelEvaluation = (params: {
  startDate?: string
  endDate?: string
  channelType?: string
}) => {
  return request({
    url: '/api/channel/evaluation',
    method: 'get',
    params
  })
}

// 获取上游渠道列表
export const getUpstreamChannels = (params: {
  page?: number
  pageSize?: number
  channelName?: string
  status?: string
  channelType?: string
}) => {
  return request({
    url: '/api/channel/upstream',
    method: 'get',
    params
  })
}

// 创建上游渠道
export const createUpstreamChannel = (data: any) => {
  return request({
    url: '/api/channel/upstream',
    method: 'post',
    data
  })
}

// 更新上游渠道
export const updateUpstreamChannel = (id: string, data: any) => {
  return request({
    url: `/api/channel/upstream/${id}`,
    method: 'put',
    data
  })
}

// 删除上游渠道
export const deleteUpstreamChannel = (id: string) => {
  return request({
    url: `/api/channel/upstream/${id}`,
    method: 'delete'
  })
}

// 批量更新上游渠道状态
export const batchUpdateUpstreamStatus = (data: {
  ids: string[]
  status: string
}) => {
  return request({
    url: '/api/channel/upstream/batch-status',
    method: 'put',
    data
  })
}

// 批量删除上游渠道
export const batchDeleteUpstreamChannels = (ids: string[]) => {
  return request({
    url: '/api/channel/upstream/batch-delete',
    method: 'delete',
    data: { ids }
  })
}

// 批量测试上游渠道
export const batchTestUpstreamChannels = (ids: string[]) => {
  return request({
    url: '/api/channel/upstream/batch-test',
    method: 'post',
    data: { ids }
  })
}

// 测试上游渠道连接
export const testUpstreamChannel = (channelId: string) => {
  return request({
    url: `/api/channel/upstream/test/${channelId}`,
    method: 'post'
  })
}

// 更新上游渠道API配置
export const updateUpstreamChannelApiConfig = (channelId: string, config: any) => {
  return request({
    url: `/api/channel/upstream/${channelId}/api-config`,
    method: 'put',
    data: config
  })
}

// 获取下游渠道列表
export const getDownstreamChannels = (params: {
  page?: number
  pageSize?: number
  channelName?: string
  status?: string
  channelType?: string
}) => {
  return request({
    url: '/api/channel/downstream',
    method: 'get',
    params
  })
}

// 创建下游渠道
export const createDownstreamChannel = (data: any) => {
  return request({
    url: '/api/channel/downstream',
    method: 'post',
    data
  })
}

// 更新下游渠道
export const updateDownstreamChannel = (id: string, data: any) => {
  return request({
    url: `/api/channel/downstream/${id}`,
    method: 'put',
    data
  })
}

// 删除下游渠道
export const deleteDownstreamChannel = (id: string) => {
  return request({
    url: `/api/channel/downstream/${id}`,
    method: 'delete'
  })
}

// 批量更新下游渠道状态
export const batchUpdateDownstreamStatus = (data: {
  ids: string[]
  status: string
}) => {
  return request({
    url: '/api/channel/downstream/batch-status',
    method: 'put',
    data
  })
}

// 批量删除下游渠道
export const batchDeleteDownstreamChannels = (ids: string[]) => {
  return request({
    url: '/api/channel/downstream/batch-delete',
    method: 'delete',
    data: { ids }
  })
}

// 批量测试下游渠道
export const batchTestDownstreamChannels = (ids: string[]) => {
  return request({
    url: '/api/channel/downstream/batch-test',
    method: 'post',
    data: { ids }
  })
}

// 测试下游渠道连接
export const testDownstreamChannel = (channelId: string) => {
  return request({
    url: `/api/channel/downstream/test/${channelId}`,
    method: 'post'
  })
}

// 批量更新下游渠道
export const batchUpdateDownstreamChannel = (data: {
  ids: string[]
  action: string
  status?: string
}) => {
  return request({
    url: '/api/channel/downstream/batch-update',
    method: 'put',
    data
  })
}

// 导出上游渠道
export const exportUpstreamChannels = (params: any) => {
  return request({
    url: '/api/channel/upstream/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// 导入上游渠道
export const importUpstreamChannels = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/api/channel/upstream/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 导出下游渠道
export const exportDownstreamChannels = (params: any) => {
  return request({
    url: '/api/channel/downstream/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// 导入下游渠道
export const importDownstreamChannels = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/api/channel/downstream/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 类型定义
export interface ChannelStatus {
  id: string
  channelName: string
  channelType: string
  connectionStatus: 'connected' | 'disconnected' | 'warning' | 'error'
  successRate: number
  avgResponseTime: number
  todayPushCount: number
  todaySuccessCount: number
  lastCheckTime: string
  apiEndpoint: string
  errorMessage?: string
}

export interface ChannelLog {
  id: string
  channelId: string
  timestamp: string
  status: 'connected' | 'disconnected' | 'warning' | 'error'
  responseTime: number
  message: string
}

export interface ChannelQualityData {
  channelId: string
  channelName: string
  channelType: string
  totalPushes: number
  successfulPushes: number
  failedPushes: number
  successRate: number
  avgResponseTime: number
  maxResponseTime: number
  minResponseTime: number
  dateRange: string
}

export interface ChannelEvaluationData {
  channelId: string
  channelName: string
  channelType: string
  score: number
  rank: string
  effectiveness: number
  stability: number
  responseQuality: number
  recommendation: string
  dateRange: string
}

// 下游渠道接口类型
export interface DownstreamChannel {
  id: string
  channelName: string
  channelType: string
  apiEndpoint: string
  webhookUrl: string
  pushMode: 'realtime' | 'batch'
  connectionStatus: 'connected' | 'disconnected'
  status: 'enabled' | 'disabled'
  successRate: number
  todayPushCount: number
  todaySuccessCount: number
  fieldMappings?: Array<{
    sourceField: string
    targetField: string
    fieldType: string
    required: boolean
  }>
  pushConfig?: {
    pushStrategy: string
    pushFrequency: string
    pushCondition: string
    batchSize: number
    retryCount: number
    retryInterval: number
    timeout: number
    dataFormat: string
    compression: string
    failureHandling: string
    enabled: boolean
  }
  remark?: string
  createTime: string
  updateTime: string
}

export interface DownstreamChannelForm {
  id?: string
  channelName: string
  channelType: string
  apiEndpoint: string
  webhookUrl: string
  pushMode: 'realtime' | 'batch'
  apiKey?: string
  status: 'enabled' | 'disabled'
  remark?: string
}

// 上游渠道接口类型
export interface UpstreamChannel {
  id: string
  channelName: string
  channelType: string
  apiEndpoint: string
  apiKey: string
  dailyLimit: number
  priority: number
  status: 'enabled' | 'disabled'
  connectionStatus: 'connected' | 'disconnected'
  successRate: number
  todayPullCount: number
  todaySuccessCount: number
  apiConfig?: {
    headers: Array<{
      key: string
      value: string
    }>
    timeout: number
    retryCount: number
    retryInterval: number
    responseFormat: 'json' | 'xml' | 'form'
    responsePath: string
    statusField: string
    statusSuccessValue: string
    messageField: string
  }
  remark?: string
  createTime: string
  updateTime: string
}

export interface UpstreamChannelForm {
  id?: string
  channelName: string
  channelType: string
  apiEndpoint: string
  apiKey: string
  dailyLimit: number
  priority: number
  status: 'enabled' | 'disabled'
  remark?: string
}

// 获取渠道质量分析数据
export const getChannelQualityAnalysis = (params: {
  startDate: string
  endDate: string
}) => {
  return request({
    url: '/api/channel/quality-analysis',
    method: 'get',
    params
  })
}